<?php
/**
 * Created by PhpStorm.
 * Admin: Administrator
 * Date: 2018/7/28/028
 * Time: 18:10
 */


namespace App\Http\Controllers\Order;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Service\Product\ProductService;
use App\Http\Service\Order\OrderService;
use Session,Config;
use Cache;
use Excel;

class ProductOrderController extends Controller
{
    /**
     * @return view
     * 套餐订单列表页面
     */
    public function index()
    {
        $packlist = $this->getPackageList();
        return view('Order.product_order')->with('packlist',$packlist);
    }


    /**
     * @param Request $request
     * 套餐订单列表
     */
    public function getProOrderList(Request $request)
    {

        $where= $request->except('_token');

        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));
        if($request->has('page'))
        {
            $where['page_no'] = $request->input('page');
            unset($where['page']);
        }
        if($request->has('limit'))
        {
            $where['page_size'] = $request->input('limit');
            unset($where['limit']);
        }else{
            $where['page_size'] = 10;
        }


        foreach($where as $key=>$value)
        {
            if($value =='' || $value == 'undefined')
            {
                unset($where[$key]);
            }
        }

        $Order = new OrderService();
        $data = $Order->getProOrderList($where);

        if($data)
        {
            return $data;
        }



    }



    /**
     * @param Request $request
     * 导出excel
     */
    public function downloadProOrderList(Request $request)
    {


        $where= $request->except('_token');

        $where['token'] = Session::get(Config::get('custom.setting.MEMBER_TOKEN'));
        if($request->has('page'))
        {
            $where['page_no'] = $request->input('page');
            unset($where['page']);
        }
        if($request->has('limit'))
        {
            $where['page_size'] = $request->input('limit');
            unset($where['limit']);
        }else{
            $where['page_size'] = 10;
        }


        foreach($where as $key=>$value)
        {
            if($value =='' || $value == 'undefined')
            {
                unset($where[$key]);
            }
        }

        $Order = new OrderService();
        $data = $Order->getProOrderList($where);

        if($data && !empty($data['data']))
        {
            foreach ($data['data'] as $k => &$value)
            {
                unset($value['order_type']);
                unset($value['order_original']);
                unset($value['pay_type']);
            }

            $cellData = $data['data'];

            $title = ['ID','订单号','手机号','套餐名称','订单金额(元)','创建时间','支付类型','订单来源',];

            array_unshift($cellData,$title);

            Excel::create('套餐订单列表',function($excel) use ($cellData){
                $excel->sheet('set_meal_order', function($sheet) use ($cellData){
                    $sheet->rows($cellData);
                });
            })->export('xlsx');
        }


    }


    public function getPackageList(){
        $where = [];
        $product = new ProductService();
        $data = $product->getProductInfoList($where);

        if($data['error_code'] == 0)
        {
            $ret = $data['data'];
        } else {
            $ret = [];
        }
        return $ret;
    }
}